package com.clh.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clh.dto.Result;
import com.clh.entity.FamilyType;
import com.clh.service.FamilyTypeService;
import com.clh.service.impl.FamilyTypeServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class FamilyTypeServlet
 */
@WebServlet("/FamilyTypeServlet")
public class FamilyTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private FamilyTypeService familyTypeService = new FamilyTypeServiceImpl();  
    

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if("getList".equals(op)) {
			doGetTypeList(request,response);
		}else if("addType".equals(op)) {
			doAddType(request,response);
		}
		
		
	}



	/* @Description:（方法功能描述）
	* @param request
	* @param response（展示方法参数和返回值）
	*/
	private void doAddType(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}



	/* @Description:（获取家庭成员类型表）
	* @param request
	* @param response（展示方法参数和返回值）
	*/
	private void doGetTypeList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//设置统一的返回对象
		Result<List<FamilyType>> result=new Result<List<FamilyType>>();

		//调用业务层
		List<FamilyType> list = familyTypeService.getFamilyTypeList();
		
		if(list==null) {
			result.fail(0, "获取家庭成员类型表失败！");
		}else {
			result.ok(list);
		}
		
		//设置响应
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
	}

}
